package models;
import java.text.Format;
import java.text.ParseException;
import java.util.Date;
import javax.persistence.*;
import com.avaje.ebean.ExpressionList;
import com.avaje.ebean.Page;
import play.data.format.Formats;
import play.data.validation.Constraints;
import play.db.ebean.Model;
import controllers.JsonDateFormatter;
import controllers.JsonDatetimeFormatter;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import controllers.Helper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import play.libs.Json;
import models.Driver;
import models.Vehicle;
import models.Staff;
@Entity
@Table(name="repairrequest")
public class Repairrequest extends Model {
	private static final long serialVersionUID = 1L;
	@Id
	public Integer id; //编号
	@ManyToOne
	public Driver driver_reporter; //报修人
	@ManyToOne
	public Vehicle vehicle; //报修车号
	@JsonSerialize(using=JsonDateFormatter.class)
	public Date request_date; //报修日期
	@ManyToOne
	public Staff staff_accepter; //受理人
	
	public String repair_type; //报修属性
	
	public String repair_content; //报修内容
	
	public String check_comments; //受理人审核意见及安排
	@JsonSerialize(using=JsonDateFormatter.class)
	public Date send_date; //送修日期
	
	public String send_company; //送修单位
	@JsonSerialize(using=JsonDateFormatter.class)
	public Date finish_date; //竣工日期
	@JsonSerialize(using=JsonDateFormatter.class)
	public Date receive_date; //提车日期
	@ManyToOne
	public Driver driver_receive_person; //提车人
	
	public String status; //转维修
	public static Model.Finder<Integer, Repairrequest> find = new Model.Finder<Integer, Repairrequest>(Integer.class, Repairrequest.class);

	public static Page<Repairrequest> page(int page, int pageSize, String sortBy, String order, String [] filterColum, String[] filter) {
		ExpressionList<Repairrequest> exp = find.where();
		for(int i= 0; i< filterColum.length; i++) {
			String[] tokens = filter[i].split("\\|", 3);
			if (tokens.length == 0) {
				continue;
			}
			String fieldtype = tokens[0];
			if (tokens.length == 3) { //two values, range search
				if (tokens[1].isEmpty() || tokens[2].isEmpty() ) {
					continue;
				}
				Object obj0 = Helper.stringToObject(fieldtype, tokens[1]);
				Object obj1 = Helper.stringToObject(fieldtype, tokens[2]);
				exp = exp.between(filterColum[i], obj0, obj1);
			} else if (tokens.length == 2) { //one value search
				if (tokens[1].isEmpty()) {
					continue;
				}
				if (fieldtype.equalsIgnoreCase("String")) {
					exp = exp.ilike(filterColum[i], "%" + tokens[1] + "%");
				} else {
					Object obj0 = Helper.stringToObject(fieldtype, tokens[1]);
					exp = exp.eq(filterColum[i], obj0);
				}
			}
		}
		return exp.orderBy(sortBy + " " + order).findPagingList(pageSize).setFetchAhead(false).getPage(page);
	}
	public void setId(Integer d) {
		id= d;
	}
	public Integer getId() {
		return id;
	}
	public void setDriver_reporter(Driver d) {
		driver_reporter= d;
	}
	public Driver getDriver_reporter() {
		return driver_reporter;
	}
	public void setVehicle(Vehicle d) {
		vehicle= d;
	}
	public Vehicle getVehicle() {
		return vehicle;
	}
	public void setRequest_date(Date d) {
		request_date= d;
	}
	public Date getRequest_date() {
		return request_date;
	}
	public void setStaff_accepter(Staff d) {
		staff_accepter= d;
	}
	public Staff getStaff_accepter() {
		return staff_accepter;
	}
	public void setRepair_type(String d) {
		repair_type= d;
	}
	public String getRepair_type() {
		return repair_type;
	}
	public void setRepair_content(String d) {
		repair_content= d;
	}
	public String getRepair_content() {
		return repair_content;
	}
	public void setCheck_comments(String d) {
		check_comments= d;
	}
	public String getCheck_comments() {
		return check_comments;
	}
	public void setSend_date(Date d) {
		send_date= d;
	}
	public Date getSend_date() {
		return send_date;
	}
	public void setSend_company(String d) {
		send_company= d;
	}
	public String getSend_company() {
		return send_company;
	}
	public void setFinish_date(Date d) {
		finish_date= d;
	}
	public Date getFinish_date() {
		return finish_date;
	}
	public void setReceive_date(Date d) {
		receive_date= d;
	}
	public Date getReceive_date() {
		return receive_date;
	}
	public void setDriver_receive_person(Driver d) {
		driver_receive_person= d;
	}
	public Driver getDriver_receive_person() {
		return driver_receive_person;
	}
	public void setStatus(String d) {
		status= d;
	}
	public String getStatus() {
		return status;
	}
	public static Repairrequest findById(Integer d) {
		return find.byId(d);
	}
	public static String getIdJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Repairrequest v: find.all()) {
			node.put(v.getId().toString(), v.getId().toString()); 
		}
		return node.toString();
	}
}
